package com.etc.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.etc.controller.LoginController;
import com.etc.controller.SerializeOperation;
import com.etc.entity.Counter;

public class SwingWindow {
	
	LoginController loginController = new LoginController();
    private DataInputStream dis;
    private DataOutputStream dos;
	
	public static void main(String[] args) {
		
		entrance();
	}


	public static void entrance() {
		Socket sock;
		DataOutputStream dos = null;
		DataInputStream dis = null;
		try {
			sock = new Socket("localhost", 6700);
			dos = new DataOutputStream(sock.getOutputStream());
			dis = new DataInputStream(sock.getInputStream());
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}			
		
		JFrame frame=new JFrame("PK游戏");
		SwingWindow swingWindow = new SwingWindow();
		frame.setSize(335, 225);

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel panel=new JPanel();
		//添加面板
		frame.add(panel);
		
		swingWindow.placeComponents(panel, frame, dos, dis);
		
		frame.setVisible(true);
	}
	
	
	private void placeComponents(JPanel panel, JFrame frame, DataOutputStream dos, DataInputStream dis) {
		panel.setLayout(null);
		panel.setBackground(Color.lightGray);
		JLabel userLabel = new JLabel("账号：");
		
		userLabel.setBounds(50, 20, 80, 25);
		panel.add(userLabel);
		
//		创建文本域用于用户输入
		JTextField userText = new JTextField(20);
		userText.setBounds(100, 20, 165, 25);
		panel.add(userText);
		
//		输入密码的文本域
		JLabel pwdLabel = new JLabel("密码：");
		pwdLabel.setBounds(50, 50, 80, 25);
		panel.add(pwdLabel);

		JPasswordField pwdText = new JPasswordField(20);
		pwdText.setBounds(100, 50, 165, 25);
		panel.add(pwdText);
		
//		创建登录按钮
		JButton loginButton = new JButton("登录");
		loginButton.setBounds(50, 125, 80, 25);
		panel.add(loginButton);
		
		//创建注册按钮
		JButton registerButton = new JButton("注册");
		registerButton.setBounds(200, 125, 80, 25);
		panel.add(registerButton);
		
		loginButton.addActionListener(e->{
			String userId = userText.getText();
			String password = pwdText.getText();
			
			//发送消息
			String restr = sendData("login#" + userText.getText() + "#" + pwdText.getText(), panel, dos, dis);
			if(restr.equals("error")|| restr==null) {
				JOptionPane.showMessageDialog(panel, "用户名或密码错误", "登录失败", JOptionPane.ERROR_MESSAGE);

			} else {
				Counter counter = SerializeOperation.loadFromFile(restr);
				GameLoading gl=new GameLoading(counter, dos, dis, panel);
				gl.setTitle(counter.getLoginId());
				gl.pack();
				gl.setVisible(true);
				gl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				gl.setLocationRelativeTo(null);
				frame.setVisible(false);
			}
			
		});
		
		registerButton.addActionListener(e->{
			JFrame jframe = new JFrame("注册界面");
			jframe.setBounds(100, 100, 400, 300);
			jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			Register register = new Register();
			JPanel panel2=new JPanel();
//			添加面板
			jframe.add(panel2);
			register.placeComponents(panel2, dos, dis, jframe);
			jframe.setVisible(true);
			frame.setVisible(false);
		});
	}
	
	
	public String sendData(String str, JPanel panel,  DataOutputStream dos, DataInputStream dis) {
        try {
            dos.writeUTF(str);
            dos.flush();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(panel, "发送数据失败，可能连接已断开", "ERROR", JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
            System.exit(0);
        }

        String retStr = null;

        try {
            retStr = dis.readUTF();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(panel, "接收数据失败，可能连接已断开", "ERROR", JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
            System.exit(0);
        }
        return retStr;
    }
	
}
